package com.zhijiang.hospital.controller;

import com.alibaba.excel.EasyExcel;
import com.zhijiang.config.result.R;
import com.zhijiang.config.webConfig.PassToken;
import com.zhijiang.hospital.entity.Checkin;
import com.zhijiang.hospital.entity.dto.AisOperRegDto;
import com.zhijiang.hospital.service.CheckinService;
import com.zhijiang.util.BaseQuery;
import com.zhijiang.util.PageList;
import com.zhijiang.util.Result;
import com.zhijiang.util.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(value = "打卡运行数据", tags = "打卡运行数据")
@RestController
@RequestMapping("Checkin")
public class CheckController {


    //打卡数据
    @Resource
    CheckinService checkinService;


    @ApiOperation("打卡数据")
    @ApiModelProperty(value = "打卡数据")
    @PostMapping("selectCheckDate")
    public Result selectCheckDate(@RequestBody BaseQuery baseQuery) {
        List<Checkin> checkDate = checkinService.getCheckDate(baseQuery);
        long size = Long.valueOf(checkDate.size());
        List<Checkin> collect = checkDate
                .stream().skip((baseQuery.getPage() - 1) * baseQuery.getPageSize()).limit(baseQuery.getPageSize()).collect(Collectors.toList());
        PageList<Checkin> checkinPageList = new PageList<Checkin>();
        checkinPageList.setPage(baseQuery.getPage());
        checkinPageList.setList(collect);
        checkinPageList.setPageSize(baseQuery.getPageSize());
        checkinPageList.setTotal(size);
        int pages = (int) (size % baseQuery.getPageSize() == 0 ? size / baseQuery.getPageSize() : size / baseQuery.getPageSize() + 1);
        checkinPageList.setPages(pages);
        return Result.ok(checkinPageList);
    }

    //导出
    @PostMapping("loadExcel")
    public void loadExcel(@RequestBody BaseQuery baseQuery, HttpServletResponse response) throws IOException {

        List<Checkin> checkDate = checkinService.getCheckDate(baseQuery);

        log.error("导出指定的住院绩效考核数据", checkDate);
        String title = "导出指定的住院绩效考核数据";
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("住院绩效考核.xlsx", "utf-8"));
        //4. 创建输入、输出流
        EasyExcel.write(response.getOutputStream(), Checkin.class).sheet(title).doWrite(checkDate);
    }


}
